<template>
  <div class="uesr-container">
    <!--已登录显示-->
    <div v-if="user" class="uesr-info login-true">
      <div class="uesr-base">
        <div class="left">
          <van-image class="avatar" round fit="cover" width="60" height="60" :src="userInfo.photo" />
          <span>{{userInfo.name}}</span>
        </div>
        <div class="right"><van-button size="mini" round to="/uesrProfile">编辑资料</van-button></div>
      </div>
      <div class="uesr-data">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!--未登录显示-->
    <div v-else class="uesr-info not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <i class="toutiao toutiao-wode-copy"></i>
        <span>未登录</span>
      </div>
    </div>
    <!--宫格导航-->
    <van-grid class="grid-nav mb-9" :column-num="2" clickable>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link/>
    <van-cell title="小聪同学" class="mb-9" is-link @click="$router.push('/websocket')" />
    <van-cell v-if="user" title="退出登录" class="logout-cell" @click="onLogout" is-link />
  </div>
</template>

<script>
// 用容器的 mapState 方法映射到组件中
import { mapState } from 'vuex'
// 调用用户信息 api
import { getUserInfo } from '@/api/user'

export default {
  name: 'Uesr',
  components: {},
  data () {
    return {
      userInfo: {} // 用户信息
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    // 如果用户登录了，则请求加载用户信息数据
    if (this.user) {
      this.loadUserInfo()
    }
  },
  methods: {
    // 退出登录
    onLogout () {
      // console.log('onLogout')
      // 退出提示
      // 在组件中需要使用 this.$dialog 来调用弹框组件
      this.$dialog.confirm({
        title: '退出提示',
        message: '确认退出登录吗？'
      }).then(() => {
        console.log('确认执行这里')
        // 确认退出：清除登录状态（容器中的 user + 本地存储中的 user）
        this.$store.commit('setUser', null)
      }).catch(() => {
        console.log('取消执行这里')
      })
    },
    // 用户信息
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        console.log(data)
        this.userInfo = data.data
      } catch (err) {
        this.$toast('获取数据失败，请稍后重试！')
      }
    }
  }
}
</script>

<style scoped lang="less">
.uesr-container{
  width: 100%;
}
.uesr-info{
  padding: 1.8rem 20px;
  background: url("~@/assets/userBg.jpg");
  background-size: cover;
  border-radius: 0 0 20px 20px;
  overflow: hidden;
  box-sizing: border-box;
}
.login-btn{
  display: flex;
  align-items: center;
  font-size: 20px;
  color: #fff;
  i{
    width: 40px;
    height: 40px;
    background-color: #eee;
    border-radius: 50%;
    margin-right: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .toutiao-wode-copy:before{
    color: #666;
  }
}
.login-true{
  padding-top: 0;
  padding-bottom: 0;
}
.uesr-base{
  padding-top: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .left{
    display: flex;
    align-items: center;
    font-size: 18px;
    color: #fff;
    span{
      margin-left: 10px;
    }
  }
}
.uesr-data{
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: .5rem;
  padding-bottom: .4rem;
  font-size: 16px;
  color: #fff;
  .data-item{
    text-align: center;
    span{
      display: block;
    }
  }
}
.van-grid-item{
  .toutiao{
    font-size: 26px;
  }
  .toutiao-shoucang{
    color: #eb5232;
  }
  .toutiao-lishi{
    font-size: 21px;
    color: #ff9d1d;
  }
  .text{
    font-size: 16px;
  }
}
</style>
